#include "spectrumtask.h"

#include <QTextStream>
#include <QtDebug>
#include <QDir>

#include "plogger.h"
#include "SpecMake.h"

SpectrumTask::SpectrumTask(QObject *parent) :
    QTask(parent)
{
}

SpectrumTask::SpectrumTask(QCoreApplication *app, QObject *parent) :
    QTask(app,parent,"spectrum")
{
}

bool SpectrumTask::runInteractive (QuitRole role)
{
    // Eseguo runInteractive QTask::runInteractive come figlio
    if (!QTask::runInteractive(CHILD))
        return false;
    lineOut(tr("(SpectrumTask) [EXE]: START RUN: mode INTERACTIVE ..."),CH_BOTH);
    /********************************************/
    /* start --> SpectrumTask::runInteractive() */
    /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
    lineOut(tr("(SpectrumTask) [EXE]:     1) Leggo il file main ..."),CH_BOTH);
    bool success = readMainPage();
    if (!success)
        return false;
    lineOut(tr("(SpectrumTask) [EXE]:        ... OK"),CH_BOTH);

    // Memorizzo la pagina di input
    lineOut(tr("(SpectrumTask) [EXE]:     2) Memorizzo il file main."),CH_BOTH);
    m_mainPage = getMainPage();

    // Memorizzo la pagina di input
    lineOut(tr("(SpectrumTask) [EXE]:     3) Costruisco il solutore [SpecMake]."),CH_BOTH);
    SpecMake spectrum;

    // Traduco il file main
    lineOut(tr("(SpectrumTask) [EXE]:     4) Traduco il file main ..."),CH_BOTH);
    if (!spectrum.solverImport(m_mainPage))
    {
        lineOut(tr("(SpectrumTask) [ERR]:        ... errore occorso nella traduzione !!!"),CH_BOTH);

        // Salvo la pagina di log
        spectrum.solverReport(m_logPage);
        saveLogPage(m_logPage);

        lineOut(tr("(SpectrumTask) [INF]:        ... controlla il file LOG dell'elaborazione !!!"));
        return false;
    }
    lineOut(tr("(SpectrumTask) [EXE]:        ... OK"),CH_BOTH);

    // Traduco il file main
    lineOut(tr("(SpectrumTask) [EXE]:     5) Lancio il solutore ..."),CH_BOTH);
    if (!spectrum.solve())
    {
        lineOut(tr("(SpectrumTask) [ERR]:        ... errore occorso nel solutore !!!"),CH_BOTH);

        // Salvo la pagina di log
        spectrum.solverReport(m_logPage);
        saveLogPage(m_logPage);

        lineOut(tr("(SpectrumTask) [INF]:        ... controlla il file LOG dell'elaborazione !!!"));
        return false;
    }
    lineOut(tr("(SpectrumTask) [EXE]:        ... OK"),CH_BOTH);

    /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
    /*   end --> SpectrumTask::runInteractive() */
    /********************************************/
    // Salvo la pagina di log
    spectrum.solverReport(m_logPage);
    saveLogPage(m_logPage);

    lineOut(tr("(SpectrumTask) [EXE]: ..END RUN: mode INTERACTIVE !!!"),CH_BOTH);
    return true;
}

bool SpectrumTask::runArguments(QuitRole role)
{
    // Eseguo runInteractive QTask::runInteractive come figlio
    if (!QTask::runArguments(CHILD))
        return false;
    lineOut(tr("(SpectrumTask) [EXE]: START RUN: mode ARGUMENTS ..."),CH_LOG);
    /********************************************/
    /* start --> SpectrumTask::runArguments()   */
    /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/

    /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
    /*   end --> SpectrumTask::runArguments()   */
    /********************************************/
    lineOut(tr("(SpectrumTask) [EXE]: ..END RUN: mode ARGUMENTS !!!"),CH_LOG);
    // quit();
    return true;
}

